define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Echarts) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'survey.records/index' + location.search,
                    // add_url: 'survey.records/add',
                    // edit_url: 'survey.records/edit',
                    // del_url: 'survey.records/del',
                    // multi_url: 'survey.records/multi',
                    // import_url: 'survey.records/import',
                    table: 'survey_records',
                }
            });

            var table = $("#table");
			var columns = [
                // {checkbox: true},
                {field: 'id', title: __('Id')},
                {field: 'customer.name', operate: 'like', title: __('客户')},
                {field: 'user.nickname', title: __('用户')},
                {field: 'add_time', title: __('填写时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                {field: 'user.phone', title: '手机号'},
                // {field: 'survey_id', title: __('Survey_id')},
                // {field: 'is_del', title: __('Is_del')},
            ]

	        Config.showQuestion.forEach(function(item, index){
				columns.push({
					field: 'question' + index, title: item.question_name, operate: false,
					formatter: (value, row, index)=>{
						let val = '';
						row.answer.forEach(function(a){
							if(a.question_id == item.id){
								val = a.question_value
							}
						})
						return val;
					}
				})
	        })

	        columns.push({field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
				buttons: [
					{
	                    name: 'detail',
	                    text: __('Detail'),
	                    icon: 'fa fa-list',
	                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
	                    url: 'survey.records/answers/id/{id}'
					},
					/*{
	                    name: 'review',
	                    text: __('报告'),
	                    icon: 'fa fa-info',
	                    classname: 'btn btn-success btn-xs btn-detail btn-dialog',
	                    url: 'survey.records/review/id/{id}'
					},*/
				],
			})

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
	            fixedColumns: true,
	            fixedRightNumber: 1,
                columns: [columns]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
	    answers: function(){
			$('.answer-image').click(function(){
				var data = [];
				$('.answer-image').each(function(){
					var src = $(this).attr('src');
					var url = Fast.api.cdnurl(src);
					data.push({
                        src: url,
                        thumb: url
                    });
				})
				Layer.photos({
                    photos: {
                        "start": $(this).index('.answer-image'),
                        "data": data
                    },
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
			})

		    // 生成图表
		    var chartDom = document.getElementById('echart');
		    var myChart = Echarts.init(chartDom);
			var indicator = [];
			var dataValue = [];
			review.datas.forEach(function(value){
				indicator.push({name: value.title, max: 10})
				dataValue.push(value.score);
			})

			var option = {
			  radar: {
				shape: 'circle',
				indicator: indicator
			  },
			  series: [
				{
				  type: 'radar',
				  data: [
					{
					  value: dataValue,
					},
				  ]
				}
			  ]
			};

			option && myChart.setOption(option);

			var option = {
				grid: {left: 100, right: 10, top: 0, bottom: 30},
				yAxis: {type: 'category', data: []},
				xAxis: {type: 'value', max: 10},
				series: [{data: [], type: 'bar'}]
			}

			var lineDom = document.getElementsByClassName('bars');
			Array.from(lineDom).forEach(function(dom,i){
				var myChart = Echarts.init(dom);
				var titles = [];
				var values = [];
				review.datas[i].info.forEach(function(value){
					if(value.score <=3){
						var color = '#fe0000';
					}else if(value.score <= 6){
						var color = '#ffc200';
					}else{
						var color = '#91d153';
					}
					titles.push(value.title)
					values.push({name: value.title, label: {show:true, formatter:(params) => params.value + '分'}, value: value.score, itemStyle: {color: color}});
				})
				option.yAxis.data = titles;
				option.series[0].data = values;
				option && myChart.setOption(option);
			})
	    },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
